<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:fn="http://java.sun.com/jsp/jstl/functions"
	xmlns:p="http://primefaces.org/ui">




<ui:composition template="/templates/internal.xhtml">
	<ui:define name="center">

	<script type="text/javascript">
	//<![CDATA[
	        

	    function StartWorkout(selector){
	    	$(selector).each(function( index ) {

	    		var startDate 	=  $( this ).find(".startDate");	  
	    		var endDate		=  $( this ).find(".endDate");	  
				
	    		var workoutHeader = "";
				var workoutBody   = "";
	    		
				workoutHeader = WorkoutHeader(startDate,endDate);					    		

				this.append(workoutHeader);
	    		  
	    	});
	    }       
	    
	    
	           
	    function DaysBetween(beg,end){
	    	var oneDay = 24 * 60 * 60 * 1000;

	    	var diffDays = Math.round(Math.abs((beg.getTime() - end.getTime())/(oneDay)));	           

			return diffDays;
	    };       
	    
	    function WeeksOfYear(beg,end){
	    	if(beg.getFullYear() < end.getFullYear()){
				var aux = new Date(beg.getFullYear(), 11, 31);
	    		while($.datepicker.iso8601Week(aux) == 1){
					aux.setDate(aux.getDate() - 1);	    			
	    		};
	    		
		    	return $.datepicker.iso8601Week(aux) -  $.datepicker.iso8601Week(beg);
	    	}else{
		    	return $.datepicker.iso8601Week(end) -  $.datepicker.iso8601Week(beg);
	    	}
	    };       
	    

	    function Header(name,colspan){
			var h = '<tr><th colspan= "' + 
			colspan +
			'" class="ui-widget-header" role="columnheader" > ' +
			name +
			'</th></tr>';
			return h;
	    }
	           
		function Button(name,beg,end){
	        var s = '<button value="' + beg.getDate() + beg.getMonth() + beg.getFullYear() + '"' +
			'class="ui-button ui-button-text-only scheduleWeek " ' +	
	        ' onclick="window.alert(this.value)">' +
    		$.datepicker.iso8601Week(beg) +
    		'</button>';
    		return s;
			
		};
	           
		function teste() {
		    var beg = new Date('Jan 1, 2014 00:00:00');
		    var end = new Date('Jan 1, 2015 00:00:00');
		    var yesterday = new Date();
		    yesterday.setDate(beg.getDate() - 1);
			var buttons = "";
		    
		    $('#scheduleTable').append(Header('Teste',WeeksOfYear(beg,end)));		    
		    
		    while( beg.getDate() 		!= end.getDate()   ||
		           beg.getMonth() 		!= end.getMonth()  ||
		           beg.getFullYear() 	!= end.getFullYear() ){

		        if( $.datepicker.iso8601Week(beg) !=  $.datepicker.iso8601Week(yesterday)  ){
			        
		        	buttons = buttons + Button($.datepicker.iso8601Week(beg),beg,beg);
				    
			        yesterday.setTime(beg.getTime());
		        }
		        beg.setDate(beg.getDate() + 1);
		    }


		    $('#scheduleTable').append('<tr><td>' + buttons + '</td></tr>' );
		    
		};
		
		
		
		$(document).ready(function () {
			teste();
		});	
	//]]>	
	</script>

	<h:form prependId="false">
	
		<div class="workoutPanel">
		</div>

		<table id="scheduleTable" >

		</table>
			


	</h:form>

	</ui:define>

	<ui:define name="menu">
	</ui:define>

	<ui:define name="tools">
	</ui:define>

	<ui:define name="dialogs">
	</ui:define>
</ui:composition>

</html>